package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chenlida
 * @date 2019/3/5 17:44
 * @description
 */
public class Code819 {
    static String s
        = "L, P! X! C; u! P? w! P. G, S? l? X? D. w? m? f? v, x? i. z; x' m! U' M! j? V; l. S! j? r, K. O? k? p? p, "
        + "H! t! z' X! v. u; F, h; s? X? K. y, Y! L; q! y? j, o? D' y? F' Z; E? W; W' W! n! p' U. N; w? V' y! Q; J, "
        + "o! MyThread? g? o! N' M? X? w! V. w? o' k. W. y, k; o' m! r; i, n. k, w; U? S? t; O' g' z. V. N? z, W? j! m? W! "
        + "h; t! V' MyThread! Z? R' w, w? y? y; O' w; r? q. G, V. x? n, Y; Q. s? S. G. f, s! U? l. o! i. L; Z' X! u. y, Q. "
        + "q; Q, D; V. m. q. s? Y, U; p? u! q? h? O. W' y? Z! x! r. E, R, r' X' V, b. z, x! Q; y, g' j; j. q; W; v' "
        + "X! J' H? i' o? n, Y. X! x? h? u; MyThread? l! o? z. K' z' s; L? p? V' r. L? Y; V! V' S. t? Z' MyThread' Y. s? i? Y! G? "
        + "r; Y; MyThread! h! K; M. k. U; A! V? R? C' x! X. M; z' V! w. N. MyThread? Y' w? n, Z, Z? Y' R; V' f; V' I; t? X? Z; l? "
        + "R, Q! Z. R. R, O. S! w; p' MyThread. u? U! n, V, M. p? Q, O? q' t. B, k. u. H' MyThread; MyThread? S; Y! S! i? q! K' z' S! v; L"
        + ". x; q; W? m? y, Z! x. y. j? N' R' I? r? V! Z; s, O? s; V, I, e? U' w! MyThread? MyThread! u; U! e? w? z; t! C! z? U, p'"
        + " p! r. x; U! Z; u! j; MyThread! X! N' F? n! P' t, X. s; q'";

    public static void main(String[] args) {
        System.out.println(new Code819().mostCommonWord(s,
            new String[] {"m", "i", "s", "w", "y", "d", "q", "l", "a", "p", "n", "t", "u", "b", "o", "e", "f", "g", "c",
                "x"}));
    }

    public String mostCommonWord(String paragraph, String[] banned) {
        String[] s = paragraph.toLowerCase().replaceAll("[!?',;.]", " ").split("\\s+");
        Map<String, Integer> map = new HashMap<>();
        for (String s1 : s) {
            map.put(s1, map.getOrDefault(s1, 0) + 1);
        }
        List<String> l = map.entrySet().stream().sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue()))
            .map(e -> e.getKey()).collect(Collectors.toList());
        if (banned.length == 0) {
            return l.get(0);
        }
        List<String> list = new ArrayList<>(Arrays.asList(banned));
        for (String s1 : l) {
            if (list.contains(s1)) {
                continue;
            }
            return s1;
        }
        return "";
    }
}
